layui.use(['layer'], function () {
    var layer = parent.layer === undefined ? layui.layer : top.layer;

    //验证当前用户是否登录
    var _ticket = $.cookie("token");
    if (_ticket != null && _ticket !== "") {
        //确认ip
        $.ajax({
            url: "/admin/user/getIp",//单点登录链接
            type: "GET",
            success: function (data) {
                var user = JSON.parse(_ticket);
                let {editCount, delCount} = {editCount: 0, delCount: 0}
                let {values} = Object;
                $.ajax({
                    url:`/admin/role-operate/tree?roleId=${user.roleId}`,
                    type:'GET',
                    async:true,
                    success(data){
                        if (data.data[0].name == "操作") {
                            if (data.data[0].children != null) {
                                let tempData = data.data[0].children;
                                for (let v of tempData) {
                                    for (let t of values(v)) {
                                        if (t === "编辑") {
                                            editCount++;
                                            break;
                                        } else if (t === "删除") {
                                            delCount++;
                                            break;
                                        }
                                    }
                                }
                                if (editCount === 0 && delCount === 0) {
                                    $('#tableListBar>a').hasClass('layui-btn-danger') === true ? $('#tableListBar>a').toggleClass('layui-btn-danger').addClass('layui-btn-disabled') : $('#tableListBar>a').addClass('layui-btn-disabled');
                                } else if (delCount === 1 && editCount === 0) {
                                    $('a#edit').hasClass('layui-btn-danger') === true ? '' : $('a#edit').addClass('layui-btn-disabled');
                                } else if (editCount === 1 && delCount === 0) {
                                    $('a#del').hasClass('layui-btn-danger') === true ? $('a#del').addClass('layui-btn-disabled') : '';
                                }
                                for (var t in tempData) {
                                    switch (tempData[t].name) {
                                        case "添加":
                                            $("blockquote.layui-elem-quote.quoteBox").append("<div class=\"layui-inline\"><a class=\"layui-btn layui-btn-normal addNews_btn\">添加</a> </div>&nbsp;");

                                            $("blockquote.layui-elem-quote.quoteBox.newProduct").append("<div class=\"layui-inline\"><button type=\"button\" class=\"layui-btn layui-btn-normal addConsumableExcel\" id=\"addConsumableExcel\">批量添加</button></div>&nbsp;");
                                            break;
                                        case "删除":
                                            $("blockquote.layui-elem-quote.quoteBox").append("<div class=\"layui-inline\"><a class=\"layui-btn layui-btn-danger layui-btn-normal delAll_btn\">批量删除</a></div>&nbsp;");
                                            break;
                                        default:
                                            break;
                                    }
                                }
                            }

                        }
                    }
                })

                if (data != user.loginIp) {
                    layer.confirm('用户登录已过期，请中重新登录', {icon: 3, title: '提示信息'}, function (tips) {
                    });
                    window.location.href = "/login";
                }
            }
        });
    } else {
        layer.confirm('用户登录已过期，请中重新登录', {icon: 3, title: '提示信息'}, function (tips) {
        });
        window.location.href = "/login";
    }

});

